import request from './utils/request'
import base from './utils/base64src'
var storage = require('./utils/storage.js')
var config = require("./utils/config.js");

App({

    setBase() {
        return new base();
    },

    onLaunch: function () {
        this.getHeight()
        this.overShare()
        this.getLand()
    },

    // 获取手机高度
    getHeight() {
        wx.getSystemInfo({
            success: e => {
                this.globalData.statusBar = e.statusBarHeight; //状态栏高度
                let custom = wx.getMenuButtonBoundingClientRect();//菜单按钮
                this.globalData.custom = custom;
                this.globalData.customBar = custom.bottom + custom.top - e.statusBarHeight;
                //计算得到定义的状态栏高度
            }
        })
    },

    getLand() {
        let token = storage.getStorage('token')
        if (!token) {
            wx.login({
                success: function (res) {
                    var code = res.code
                    wx.request({
                        url: config.domain + '/User/login',
                        data: { code },
                        method: 'GET',
                        success: function (res) {
                            if (res.data.code == 1) {
                                var user_id = res.data.data.user_id
                                storage.setStorage('user_id', user_id)
                                storage.setStorage('token', res.data.data.token)
                            }
                        }
                    })
                }
            })
        }
    },

    overShare: function () {
        //间接实现全局设置分享内容
        wx.onAppRoute(function (res) {
            //获取加载的页面
            let pages = getCurrentPages()
            //获取当前页面的对象
            let view = pages[pages.length - 1]
            if(view.route == 'pages/activity/newUserActivity'){
                let data = view.data
                if (!data.isOverShare) {
                    data.isOverShare = true;
                    view.onShareAppMessage = function () {
                        var user_id = storage.getStorage('user_id')
                        return {
                            title: '送你一张三日健身卡，点击帮我助力，你也可获得月卡哦',
                            imageUrl: "",
                            path: "/pages/activity/newUserHelp?scene=" + user_id,
                            success: function () {
                                wx.showToast({ title: '分享成功', icon: 'success', duration: 2000 })
                            }
                        }
                    }
                }
            }
            if (view.route == 'pages/venue/venueHome/index' || view.route == 'pages/venue/coach/index' || view.route == '/pages/content/index' || view.route == 'pages/activity/newUserActivity') {
                view = false
            }

            if (view) {
            let data = view.data;
                if (!data.isOverShare) {
                    data.isOverShare = true;
                    view.onShareAppMessage = function () {
                        //重写分享配置
                        var user_id = storage.getStorage('user_id')
                        return {
                            title: '健身就来约汗么',
                            // imageUrl:'',
                            path: "/pages/index/index?user_id=" + user_id   //分享页面地址    
                        };
                    }
                }
            }
        })
    },

    myRequest() {
        return new request();
    },

    VersionUpdate() {
        if (wx.canIUse('getUpdateManager')) {
            const updateManager = wx.getUpdateManager()
            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                if (res.hasUpdate) {
                    updateManager.onUpdateReady(function () {
                        wx.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好，是否重启应用？',
                            success: function (res) {
                                if (res.confirm) {
                                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                                    updateManager.applyUpdate()
                                }
                            }
                        })
                    })
                    updateManager.onUpdateFailed(function () {
                        // 新的版本下载失败
                        wx.showModal({
                            title: '已经有新版本了哟~',
                            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
                        })
                    })
                }
            })
        } else {
            // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
            wx.showModal({
                title: "提示",
                content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
            });
        }
    },
    onShow: function (options) {
        this.VersionUpdate() // 版本更新
    },

    globalData: {
        storage_time: 2592000,
    }
})